import {Component, OnInit} from '@angular/core';
import { Router } from '@angular/router';

import {ApiService} from '../../../services/api.service';
import {UserService} from '../../../services/user.service';

import 'rxjs/add/operator/toPromise';


@Component({
    selector: 'app-my-login',
    templateUrl: '../html/login.html',
    styleUrls: ['../css/login.css']
})

export class LoginComponent implements OnInit {

    error_msg: string;
    account: string;
    password: string;
    postFlg: boolean;
    header = new Headers({'Content-Type': 'application/json'});

    // header = new Headers({'Content-Type': 'application/x-www-form-urlencoded'});

    constructor(private apiService: ApiService,
                private userService: UserService,
                private router: Router
    ) {
    }

    ngOnInit(): void {
        // this.user={account:'',password:''};
        // this.error_msg='hahah 彼时无忧';
        console.log('weclome to login ');
    }

    doLogin(): void {
        if (!this.account) {
            return;
        }
        if (!this.password) {
            return;
        }
        if (this.postFlg) {
            return;
        }
        this.postFlg = false;
        //ajax post data
        let data;
        // data+="account="+this.account+"&password="+this.password;
        data = {id: 1, username: this.account, token: this.password};
        this.userService.user = data;
        this.router.navigate(['/welcome']);
        return;
        // let res = this.apiService.httpPost('', data);
        // console.log(res, 'fu', this.postFlg);
    }

}
